package com.lonely.order.support.detail.impl;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.lonely.order.domain.entity.SysOrder;
import com.lonely.order.domain.vo.order.OrderTagVO;
import com.lonely.order.domain.vo.order.QueryProduceOrderPageVO;
import com.lonely.order.domain.vo.split.GetSplitFactoryNameVO;
import com.lonely.order.enums.OrderEnum;
import com.lonely.order.service.IOrderSplitService;
import com.lonely.order.service.IOrderTagService;
import com.lonely.order.support.detail.GetOrderDetail;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author : 黄志标
 * @date: 2025/7/16 15:36
 * @description:
 */
public abstract class AbstractGetOrderDetail implements GetOrderDetail {

    @Autowired
    private IOrderTagService orderTagService;

    @Autowired
    private IOrderSplitService orderSplitService;

    /**
     * 提取订单对应的标签集合
     *
     * @param sysOrder
     * @return
     */
    protected List<QueryProduceOrderPageVO.Tag> extractOrderTags(SysOrder sysOrder) {
        List<QueryProduceOrderPageVO.Tag> tags = new ArrayList<>();

        List<OrderTagVO> orderTagVOS = this.orderTagService.queryOrderTagsByOrderIds(Lists.newArrayList(sysOrder.getId()));
        // 订单标签
        if (CollUtil.isNotEmpty(orderTagVOS)) {
            orderTagVOS.forEach(orderTag -> {
                tags.add(new QueryProduceOrderPageVO.Tag()
                        .setTagId(orderTag.getTagId())
                        .setTagName(orderTag.getTagName())
                        .setColor(orderTag.getTagColorType())
                        .setIsOrderTag(true));
            });
        }

        // 基础标签
        if (Objects.equals(OrderEnum.IsFirstOrder.FIRST_ORDER.getType(), sysOrder.getIsFirst())) {
            tags.add(new QueryProduceOrderPageVO.Tag().setTagName("首").setColor("info"));
        }
        if (Objects.equals(OrderEnum.IsFirstOrder.RETURN_ORDER.getType(), sysOrder.getIsFirst())) {
            tags.add(new QueryProduceOrderPageVO.Tag().setTagName("返").setColor("info"));
        }
        tags.add(new QueryProduceOrderPageVO.Tag().setTagName(sysOrder.getStockType()).setColor("warning"));
        tags.add(new QueryProduceOrderPageVO.Tag().setTagName(sysOrder.getOrderType()).setColor("info"));

        return tags;
    }

    /**
     * 获取订单的分单工厂信息
     *
     * @param orderId
     * @return
     */
    protected GetSplitFactoryNameVO getSplitFactoryNameInfo(Long orderId) {
        Map<Long, GetSplitFactoryNameVO> splitOrderFactoryNameMap = this.orderSplitService.getSplitFactoryNameByOrderIds(Lists.newArrayList(orderId));
        return splitOrderFactoryNameMap.get(orderId);
    }

}
